package oris.outros;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

/**
 *
 * @author u07459
 */
public class PlayerThread {

    static VoiceManager _voiceManager;
    static Voice _voice;
    static String _texto = new String();
    static boolean _pause = false;
    static boolean _play = false;
    static boolean _playando = false;
    static boolean _isPaused = false;

    public PlayerThread() {
        _voiceManager = VoiceManager.getInstance();
    }

    public void Fale(String texto, int pitch, float volume) {
        try {
            Stop();
        } catch (Exception ex) {
        }

        _voice = _voiceManager.getVoice("kevin16");
        _voice.allocate();
        SetVolume(volume);
        _texto = texto;
        _play = true;
        _voice.setPitch(pitch);
        _isPaused = false;
        new Thread(new Falador()).start();
    }

    public void Pause() {
        try {
            if (!_isPaused) {
                _isPaused = true;
                _voice.getAudioPlayer().pause();
            } else {
                _isPaused = false;
                _voice.getAudioPlayer().resume();
            }
        } catch (Exception e) {
        }
    }

    public void Stop() {
        try {
            _play = false;
            _playando = false;
            _voice.deallocate();
            _isPaused = false;
        } catch (Exception ex) {
        }
    }

    public void SetVolume(float vol) {
        try {
            _voice.setVolume(vol);
        } catch (Exception ex) {
        }
    }

    private static class Falador implements Runnable {

        public void run() {
            try {
                if (_play && !_playando) {
                    _playando = true;
                    _voice.speak(_texto);
                }
            } catch (Exception exception) {
            }
        }
    }
}
